package com.cdy.a2j.controller;

import com.cdy.a2j.beans.UserEntity;
import com.cdy.a2j.beans.VersionEntity;
import com.cdy.a2j.beans.base.BaseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController // Restful 风格控制层注解
@RequestMapping("/app") // 请求根路径
public class JsonController {

    @RequestMapping(value = "/version", method = RequestMethod.GET)
    public BaseEntity<VersionEntity> getVersion() {
        VersionEntity version = VersionEntity.builder()
                .id(1)
                .version(2)
                .versionStr("2.0.0beta")
                .downloadUrl("https://files.top1buyer.com/antbabe-release-v2.6.2-129_262.apk")
                .build();

        // 外层包装
        return BaseEntity.success(version);
    }

    @PostMapping(value = "/login")
    public BaseEntity<UserEntity> login(@RequestParam String username, @RequestParam String password) {
        if (!StringUtils.hasLength(username)) return BaseEntity.failed("用户名为空!");
        if (!StringUtils.hasLength(password)) return BaseEntity.failed("密码为空!");

        if (username.equalsIgnoreCase("chendy") && password.equalsIgnoreCase("123")) {
            UserEntity user = UserEntity.builder()
                    .id(1)
                    .age(18)
                    .email("18516516353@163.com")
                    .username("chendy")
                    .password("123")
                    .gender(1)
                    .createDate(new Date())
                    .build();
            return BaseEntity.success(user);
        }

        return BaseEntity.failed("用户名或密码错误");

    }
}
